package com.ibm.framework.exception.handler.monitor;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.ibm.framework.exception.BaseException;
import com.ibm.framework.exception.handler.ExceptionInfoExtractor;
import com.ibm.framework.exception.model.ExceptionInfo;
import com.ibm.framework.exception.model.RawExceptionInfo;

/**
 * 功能描述： 异常信息事件的监听器
 * 
 * @author v_pinzhao
 * 
 */
public class DefaultMonitorMessageExtractor implements ExceptionInfoExtractor {
    /**
     * 获取被提取的信息
     * 
     * @param rawEx 被提取的信息
     * @return 异常信息
     */
    public ExceptionInfo extract(final RawExceptionInfo rawEx) {
        ExceptionInfo exceptionInfo = new ExceptionInfo();

        Throwable ex = rawEx.getThrowable();
        if (ex instanceof BaseException) {
            BaseException be = (BaseException) ex;
            exceptionInfo.setCode(be.getCode());
        }

        // set异常被拦截的时间
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String interceptTime = dateformat.format(new Date());
        exceptionInfo.setInterceptTime(interceptTime);

        exceptionInfo.setExceptionClass(ex.getClass().getName());
        exceptionInfo.setExceptionMessage(ex.getLocalizedMessage());
        return exceptionInfo;
    }
}
